package com.cloudptt.api.product.func.crsc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.body.SubscribeErrXml;
import com.mcptt.config.body.pidf.PidfXmlBody;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class CrscBroadcast extends BroadcastReceiver implements IBroadcast
{
	private Context ctx = null;
	private CrscListener listener = null;
	private String uuid = null;
	private List<FuncNumOb> funcNumObList = new Vector<>();

	public CrscBroadcast(Context ctx)
	{
		this.ctx = ctx;
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_NOTIFY));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_PUBLISH_FUNC));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_SUBSCRIBE_FUNC));
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.debug(this.getClass(), "Action is " + action);
		if (action.equals(SipManager.ACTION_SIP_NOTIFY))
		{
			if(this.listener != null)
			{
				try
				{
					String pidfXml = intent.getStringExtra(SipManager.Body_Str);
					Log.debug(this.getClass(), "ACTION_SIP_NOTIFY pidfXml = " + pidfXml);
					PidfXmlBody body = PidfXmlBody.parseXmlStr(pidfXml);
					Log.debug(this.getClass(), "ACTION_SIP_NOTIFY body = " + body.toValueString());

					int type = body.getType();
					Log.debug(this.getClass(), "ACTION_SIP_NOTIFY type = " + type + " body = " + body.toValueString());
					if(PidfXmlBody.Type_Init_Logx == type)
					{
						uuid = body.getUuid();
						List<FuncNumOb> newObList = body.getFuncNameList();
						if(newObList != null
							&& funcNumObList != null) {
							for (Iterator<FuncNumOb> itor = newObList.iterator(); itor.hasNext(); ) {
								FuncNumOb ob = itor.next();
								if(!hasFuncNumOb(ob.getFuncNum()))
								{
									funcNumObList.add(ob);
								}
							}
						}
						Log.debug(this.getClass(), "ACTION_SIP_NOTIFY uuid = " + uuid + " funcNameList = " + funcNumObList);
						this.listener.onInitLogxResult(uuid, body.getFuncNameList());
					}
					else if(PidfXmlBody.Type_Query == type)
					{
						this.listener.onQueryResult(body.getFuncNameList());
					}
					else if(PidfXmlBody.Type_Compulsory == type)
					{
						String funcNum = body.getFuncNum();
						String user = body.getUser();
						delFuncNumOb(funcNum);
						this.listener.onCompulsoryResult(funcNum,user);
					}
//					else
//					{
//						String uuid = body.getUuid();
//						String funcNum = body.getFuncNum();
//						boolean isActive = body.isActive();
//						this.listener.onBindResult(uuid, isActive, funcNum);
//					}
				}catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
		else if (action.equals(SipManager.ACTION_SIP_PUBLISH_FUNC))
		{
			if(this.listener != null) {
				int code = intent.getIntExtra(SipManager.RESULT_CODE,0);
				String errStr = intent.getStringExtra(SipManager.EXT_STR);
				Log.debug(this.getClass(), "ACTION_SIP_PUBLISH_FUNC code = " +code + " errStr = " + errStr);
				listener.onFuncPublishRsp(code,errStr);
			}
		}
		else if (action.equals(SipManager.ACTION_SIP_SUBSCRIBE_FUNC))
		{
			if(this.listener != null) {
				int code = intent.getIntExtra(SipManager.RESULT_CODE,0);
				String str = intent.getStringExtra(SipManager.EXT_STR);
				Log.debug(this.getClass(), "ACTION_SIP_PUBLISH_FUNC code = " +code + " str = " + str);
				if(str != null) {
					SubscribeErrXml errXml = SubscribeErrXml.parseXmlStr(str);
					Log.debug(this.getClass(), "ACTION_SIP_PUBLISH_FUNC errXml = " + errXml);
					if (errXml != null
							&& errXml.getMsg() != null) {
						CommDlg.showToast(context, errXml.getMsg());
					}
				}
			}
		}
	}

	public void delFuncNumOb(String funcNum)
	{
		Log.debug(this.getClass(), "delFuncNumOb funcNum = " + funcNum + " funcNumObList = " + funcNumObList);
		for(Iterator<FuncNumOb> itor = funcNumObList.iterator(); itor.hasNext();)
		{
			if(itor != null) {
				FuncNumOb newOb = itor.next();
				Log.debug(this.getClass(), "delFuncNumOb newOb = " + newOb);
				if (newOb != null
						&& newOb.isSameOne(funcNum)) {
					itor.remove();
				}
			}
		}
	}
	public boolean hasFuncNumOb(String funcNum)
	{
		Log.debug(this.getClass(), "delFuncNumOb funcNum = " + funcNum + " funcNumObList = " + funcNumObList);
		for(Iterator<FuncNumOb> itor = funcNumObList.iterator(); itor.hasNext();)
		{
			if(itor != null) {
				FuncNumOb newOb = itor.next();
				Log.debug(this.getClass(), "delFuncNumOb newOb = " + newOb);
				if (newOb != null
						&& newOb.isSameOne(funcNum)) {
					return true;
				}
			}
		}
		return false;
	}
	public void setListener(CrscListener listener) {
		Log.debug(this.getClass(), "setListener listener = " + listener);
		this.listener = listener;
	}
	public List<FuncNumOb> getFuncNumObList() {
		return funcNumObList;
	}
	public String getFuncUuid()
	{
		return uuid;
	}
	public void clearFuncNumObList()
	{
		if(funcNumObList != null) {
			this.funcNumObList.clear();
		}
	}
	public void clearResource()
	{
		this.ctx.unregisterReceiver(this);
		this.ctx = null;
	}
}
